package com.beiding.config;

import com.beiding.dao.MessageDao;
import com.beiding.listener.AmqpMessageListener;
import com.beiding.pojo.Message;
import com.beiding.utils.AmqpUtils;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MessageSendConfig {

    //使用RabbitMQ来监听任务发送

    @Bean
    public Queue landlord() {
        return AmqpUtils.createQueue("message.send.landlord");
    }

    @Bean
    public Queue tenant() {
        return AmqpUtils.createQueue("message.send.tenant");
    }


    private RabbitTemplate rabbitTemplate;

    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    private MessageDao messageDao;

    @Autowired
    public void setMessageDao(MessageDao messageDao) {
        this.messageDao = messageDao;
    }

    //用户订阅    '/user/queue/landlord'
    @RabbitListener(queues = "message.send.landlord")
    public void landlord(Message message) {
        send(message,"landlord");
    }

    //用户订阅    '/user/queue/tenant'
    @RabbitListener(queues = "message.send.tenant")
    public void tenant(Message message) {
        send(message, "tenant");
    }

    private void send(Message message, String topic) {

        //首先检查用户在哪一个zuul上线.或者是否上线
        String server = messageDao.getOnlineServer(message.getReceiver());


        System.out.println("用户上线的服务:"+server);

        message.setTopic(topic);

        if (server == null) {//如果用户未上线
            //把消息保存起来
            messageDao.saveMessage(message.getReceiver(), message.getTopic(), message.getPayload());
        } else {//用户上线

            //重新路由到用户上线的zuul服务
            rabbitTemplate.convertAndSend("message.send.queue." + server, message);

        }

    }


    @Value("${zuulId}")
    private String zuulId;


    //监听自己的队列
    @Bean
    public Queue zuulQueue() {
        return AmqpUtils.createQueue("message.send.queue." + zuulId);
    }

    @Bean
    public SimpleMessageListenerContainer messageListenerContainer(AmqpMessageListener zuul1Listener, ConnectionFactory factory) {

        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();

        container.setConnectionFactory(factory);//工厂
        container.setQueueNames("message.send.queue."+zuulId);
        container.setExposeListenerChannel(true);
        container.setConcurrentConsumers(1);//消费者个数
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);//自动确认
        container.setMessageListener(zuul1Listener);//消息处理器

        return container;

    }


}
